package indi.luckly.common.response;


import indi.luckly.common.ApiException;
import indi.luckly.common.key.ErrorKey;


import java.io.Serializable;
import java.util.Objects;

public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private String code;
    private String msg;
    private T data;


    public static <T> ResponseResult<T> ok() {
        return (new ResponseResult()).setCode(1);
    }

    public static <T> ResponseResult<T> ok(T data) {
        return (new ResponseResult<T>()).setCode(ErrorKey.SUCCEED.getValue()).setMsg(ErrorKey.SUCCEED.describe()).setData(data);
    }

    public static ResponseResult fail(ErrorKey error) {
        return (new ResponseResult()).setCode(error.getValue()).setMsg(error.describe());
    }

    public static <T> ResponseResult<T> fail(ErrorKey error, T t) {
        return (new ResponseResult()).setCode(error.getValue()).setMsg(error.describe()).setData(t);
    }

    public static <T> ResponseResult<T> fail(ApiException error) {
        return (new ResponseResult()).setCode(error.getCode()).setMsg(error.getMessage());
    }

    public static <T> ResponseResult<T> create(String code) {
        return new ResponseResult(code);
    }


    public ResponseResult(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }


    public ResponseResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ResponseResult<T> setData(T data) {
        this.data = data;
        return this;
    }


    public ResponseResult() {
    }

    public String getCode() {
        return code;
    }

    public ResponseResult<T> setCode(int code) {
        this.code = code + "";
        return this;
    }

    public ResponseResult<T> setCode(String code) {
        this.code = code;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ResponseResult<?> that = (ResponseResult<?>) o;
        return code == that.code &&
                Objects.equals(msg, that.msg) &&
                Objects.equals(data, that.data);
    }

    @Override
    public int hashCode() {

        return Objects.hash(code, msg, data);
    }

    @Override
    public String toString() {
        return "ResponseResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
